<template>
  <div id="tbooking-policies">
    <div class="card">
      <div class="card-body small">
        <form class="" id="frmSearchPolicy">
          <input
            type="textfield"
            class="form-control"
            placeholder="航司两字代码"
            size="10"
            v-model.trim="carrier"
          />
          <input
            type="textfield"
            class="form-control"
            placeholder="舱位"
            size="10"
            v-model.trim="subclass"
          />
          <input
            type="textfield"
            class="form-control"
            placeholder="出发"
            size="10"
            v-model.trim="dport"
          />
          <input
            type="textfield"
            class="form-control"
            placeholder="到达"
            size="10"
            v-model.trim="aport"
          />
          <div class="col-1">
            <button
              type="button"
              class="btn btn-primary ms-2"
              @click.stop="search()"
            >
              查找底价
            </button>
            <button
              type="button"
              class="btn btn-secondary btn-sm ms-2"
              @click.stop="reset()"
            >
              重置
            </button>

            <button
              type="button"
              class="btn btn-success btn-sm ms-5"
              @click.stop="newBottomPrice()"
            >
              新建
            </button>
          </div>

          <button
            type="button"
            class="btn btn-secondary btn-sm ms-auto"
            @click.stop="back()"
          >
            返回
          </button>
        </form>
      </div>
      <table class="table table-sm table-striped table-hover small">
        <thead>
          <tr>
            <th>航空公司</th>
            <th>舱位</th>
            <th>行程</th>
            <th>最大扣率</th>
            <th>最低价格</th>
            <th>出发机场</th>
            <th>到达机场</th>
            <th>距今天数</th>
            <th>航班号</th>
            <th></th>
            <th></th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="info in dataList">
            <td>{{ info.carrier }}</td>
            <td>{{ info.subclass }}</td>
            <td>
              <template v-if="info.tripType === 1">单程</template>
              <template v-else-if="info.tripType === 2">往返</template>
            </td>
            <td>{{ info.maxReturnPoint }}</td>
            <td>{{ info.lowLimitPrice }}</td>
            <td>{{ info.departurePort }}</td>
            <td>{{ info.arrivalPort }}</td>
            <td>{{ info.offsetDay }}</td>
            <td>{{ info.flightNo }}</td>
            <td>
              <a href="javascript:void(0)" @click="copyBottomPrice(info)"
                >复制</a
              >
            </td>
            <!-- <td><a href="javascript:updatePolicyManage({{policy.id}});">修改</a></td> -->
            <td>
              <a href="javascript:void(0)" @click="deleteBottomPrice(info.id)"
                >删除</a
              >
            </td>
          </tr>
        </tbody>
      </table>
      <div class="card-body py-0">
        <nav id="pagination-box" class="float-end">
          <my-pagination
            name="pagination"
            :row-count="sc.rowCount"
            :page-total="sc.pageTotal"
            :page-no="sc.pageNo"
            @next-page="nextPage"
            @prev-page="prevPage"
            @direct-page="directPage"
          ></my-pagination>
        </nav>
      </div>
      <div class="card-body py-0 text-muted small">
        政策与底价匹配步骤说明：<br />
        政策距今天数 = 政策中的航班开始日期 - 今天<br />
        1. 航司、舱位严格匹配；<br />
        2. 条件: 政策距今天数 >= 0、有航班号,
        则匹配航班号、出发、到达、距今天数，有匹配则返回，无匹配则继续下一步；<br />
        3. 条件: 政策距今天数 >= 0、有航班号,
        则匹配航班号、出发、到达、不限制距今天数，有匹配则返回，无匹配则继续下一步；<br />
        4. 条件: 政策距今天数 >= 0,
        则匹配无航班号、出发、到达、距今天数，有匹配则返回，无匹配则继续下一步；<br />
        5.
        匹配无航班号、无指定日期、出发、到达，有匹配则返回，无匹配则继续下一步；<br />
        6.
        匹配无航班号、无指定日期、无出发、无到达，有匹配则返回，无匹配则返回无匹配.
      </div>
    </div>

    <modal-bottom-price ref="modalBottomPrice"> </modal-bottom-price>
  </div>
</template>

<script>
import {
  searchSpecialPolicies,
  startMonitorPolicy,
  stopMonitorPolicy,
  syncPolicy,
  localDeletePolicy,
  searchPolicyBottomPrices,
  createPolicyBottomPrice,
  deletePolicyBottomPrice,
} from "@/api/tbooking.js";
import MyPagination from "@/components/my-pagination.vue";
import ModalBottomPrice from "@/components/tbooking-modal-bottom-price.vue";

export default {
  components: {
    MyPagination,
    ModalBottomPrice,
  },
  data() {
    return {
      dataList: [],

      sc: {
        rowCount: 0,
        pageNo: 1,
        pageSize: 20,
        pageTotal: 0,
      },

      policyType: 1,
      monitoring: -1,

      beginDate: "",
      endDate: "",
      status: -1,
      orderId: "",
      policyCode: "",
      carrier: "",
      subclass: "",
      dport: "",
      aport: "",
    };
  },
  mounted: function () {
    this.search();
  },
  methods: {
    back: function () {
      this.$router.go(-1);
    },
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    showLoading: function (loadingText) {
      this.$store.commit("showLoading", {
        loading: true,
        loadingText: loadingText,
      });
    },
    hideLoading: function () {
      this.$store.commit("showLoading", { loading: false });
    },
    commonShowMessage: function (jsonResult) {
      if (jsonResult.status !== "OK") {
        this.showErrMsg("失败：" + jsonResult.errmsg, "danger");
      } else {
        if (jsonResult.desc !== "") {
          this.showErrMsg(jsonResult.desc);
        } else {
          this.showErrMsg("操作成功");
        }
        this.search();
      }
    },
    deleteBottomPrice: function (id) {
      const params = {
        id,
      };

      this.showLoading();

      deletePolicyBottomPrice(
        params,
        (v) => {
          this.commonShowMessage(v);
        },
        () => {
          this.hideLoading();
        }
      );
    },
    search: function () {
      const params = {
        "sc.pageNo": this.sc.pageNo,
        "sc.pageSize": this.sc.pageSize,
        "sc.carrier": this.carrier,
        "sc.subclass": this.subclass,
        "sc.departureAirport": this.dport,
        "sc.arrivalAirport": this.aport,
      };
      searchPolicyBottomPrices(params, (jsonResult) => {
        this.dataList = jsonResult.dataList;
        this.sc = jsonResult.page;
      });
    },
    newBottomPrice: function () {
      this.$refs.modalBottomPrice
        .modal(null)
        .then((params) => {
          createPolicyBottomPrice(params, (v) => {
            this.commonShowMessage(v);
          });
        })
        .catch((ex) => {});
    },
    copyBottomPrice: function (info) {
      this.$refs.modalBottomPrice
        .modal(info)
        .then((params) => {
          createPolicyBottomPrice(params, (v) => {
            this.commonShowMessage(v);
          });
        })
        .catch((ex) => {});
    },
    reset: function () {
      this.status = -1;
      this.sc.pageNo = 1;
      this.beginDate = "";
      this.endDate = "";
      this.orderId = "";
      this.policyCode = "";
      this.carrier = "";
      this.subclass = "";
      this.dport = "";
      this.aport = "";

      this.search();
    },
    prevPage: function () {
      this.sc.pageNo = this.sc.pageNo - 1;
      if (this.sc.pageNo < 1) this.sc.pageNo = 1;
      this.search();
    },
    nextPage: function () {
      this.sc.pageNo = this.sc.pageNo + 1;
      this.search();
    },
    directPage: function (pageNo) {
      this.sc.pageNo = pageNo;
      this.search();
    },
  },
};
</script>